package com.reggie.config;

import com.reggie.interceptor.BackendInterceptor;
import com.reggie.interceptor.FrontInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

/**
 * @Author 早睡蛋
 * @Date 2022/9/17 14:10:56
 * @Desc:
 */
@Slf4j
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

    private static String[] frontURLS = new String[]{
            "/addressBook/**",
            "/order/**",
            "/orderDetail/**",
            "/category/**",
            "/dish/**",
            "/setmeal/**",
            "/shoppingCart/**",
    };

    private static String[] BackendURLS = new String[]{
            "/addressBook/**",
            "/order/**",
            "/orderDetail/**",
            "/category/**",
            "/dish/**",
            "/setmeal/**",
            "/employee/**",
    };

    private static String[] exBackendURLS = new String[]{
            "/employee/login",
            "/employee/logout",
    };

    @Value("${dishImages.dir}")
    private String dishImages;

    @Value("${setmealImages.dir}")
    private String setmealImages;

    @Autowired
    private FrontInterceptor frontInterceptor;

    @Autowired
    private BackendInterceptor backendInterceptor;

    /**
     * 静态资源映射
     *
     * @param registry
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        log.info("静态资源映射");
        registry.addResourceHandler("/backend/**").addResourceLocations("classpath:/backend/");
        registry.addResourceHandler("/front/**").addResourceLocations("classpath:/front/");
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");

        registry.addResourceHandler("/dishImages/**").addResourceLocations(
                "file:" + dishImages
        );
        registry.addResourceHandler("/setmealImages/**").addResourceLocations(
                "file:" + setmealImages
        );
    }

    @Override
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        //消息转换器
        MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
        messageConverter.setObjectMapper(new JacksonObjectMapper());
        converters.add(0, messageConverter);
    }

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(frontInterceptor)
                .addPathPatterns(frontURLS);

        registry.addInterceptor(backendInterceptor)
                .addPathPatterns(BackendURLS)
                .excludePathPatterns(exBackendURLS);
    }
}
